package cn.skycity.common.controller;

import cn.hutool.core.util.StrUtil;
import cn.skycity.common.service.ISysOperLogService;
import cn.skycity.framework.core.model.Result;
import cn.skycity.framework.core.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skycity.common.excel.utils.ExcelUtil;
import com.skycity.common.log.annotation.Log;
import com.skycity.common.log.enums.BusinessType;
import com.skycity.framework.uaa.pojo.dto.SysOperLogBo;
import com.skycity.framework.uaa.pojo.entity.SysOperLog;
import com.skycity.framework.uaa.pojo.vo.SysOperLogVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 操作日志记录 前端控制器
 * </p>
 *
 * @author YingBo.Dai
 * @since 2024-12-05
 */
@RestController
@RequestMapping("/monitor/operlog")
@Slf4j
public class SysOperLogController {

    @Autowired
    private ISysOperLogService sysOperLogService;

    @GetMapping("/page")
    public Result<Page<SysOperLog>> pageList(Page<SysOperLog> pageVo, SysOperLogBo sysOperLogBo) {
        Map<String, String> params = sysOperLogBo.getParams();
        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<SysOperLog>()
                .like(StrUtil.isNotBlank(sysOperLogBo.getTitle()), SysOperLog::getTitle, sysOperLogBo.getTitle())
                .like(StrUtil.isNotBlank(sysOperLogBo.getOperator()), SysOperLog::getOperator,  sysOperLogBo.getOperator())
                .eq(sysOperLogBo.getBusinessType()!= null, SysOperLog::getBusinessType, sysOperLogBo.getBusinessType())
                .eq(sysOperLogBo.getStatus()!= null, SysOperLog::getStatus, sysOperLogBo.getStatus())
                .between(params.containsKey("beginTime") && params.containsKey("endTime"),
                        SysOperLog::getCreateTime, DateUtils.parseDate(params.get("beginTime")),DateUtils.parseDate(params.get("endTime")))
                .orderByDesc(SysOperLog::getCreateTime);
        Page<SysOperLog> result = sysOperLogService.page(pageVo, queryWrapper);
        return Result.success(result);
    }

    @PostMapping
    public Result<Void> add(@RequestBody SysOperLog entity) {
        sysOperLogService.save(entity);
        return Result.success();
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Result<Void> clean() {
        sysOperLogService.cleanOperLog();
        return Result.success();
    }

    /**
     * 导出操作日志记录列表
     */
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
//    @SaCheckPermission("monitor:operlog:export")
    @PostMapping("/export")
    public void export(SysOperLogBo operLog, HttpServletResponse response) {
        List<SysOperLogVo> list = sysOperLogService.selectOperLogList(operLog);
        ExcelUtil.exportExcel(list, "操作日志", SysOperLogVo.class, response);
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
//    @SaCheckPermission("monitor:operlog:remove")
    @DeleteMapping("/{operIds}")
    public Result<Void> remove(@PathVariable Long[] operIds) {
        sysOperLogService.removeByIds(Arrays.asList(operIds));
        return Result.success();
    }

}
